/**
 * 
 */
package tools;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

import persistence.Message;
import persistence.ObjectFactory;
import persistence.Utilisateur;

/**
 * @author Administrateur
 *
 */
public class DataExchangeManager {

	@SuppressWarnings("unused")
	private static Logger logger;


	public DataExchangeManager() {
		Layout layout = new SimpleLayout();
		FileAppender appender;
		try {
			appender = new FileAppender(layout, "app.log");
			Logger logger = Logger.getRootLogger();
			logger.addAppender(appender);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Convert bean object to xml string
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static String beanToXMLString(Object obj) throws Exception
	{
		JAXBContext context = JAXBContext.newInstance("persistence");
		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		StringWriter sw = new StringWriter();
		marshaller.marshal(new JAXBElement(new QName("", "String"), String.class, obj), sw);
		return sw.toString();
	}

	/**
	 * Convert xml String to beans
	 * @param <T>
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Message xmlStringToBean(String obj) throws Exception
	{
		StringBuffer xmlStr = new StringBuffer(obj);
		JAXBContext jc = JAXBContext.newInstance("persistence");
		Unmarshaller u = jc.createUnmarshaller();
		ObjectFactory factory = new ObjectFactory();
		Message out = factory.createMessage();
		JAXBElement<Message> element = factory.createMessage(out);
		element = (JAXBElement<Message>) u.unmarshal(new StreamSource(new StringReader(xmlStr.toString())));
		return element.getValue();
	}

	/**
	 * Send a request to a server
	 * @param outputStream - the outputStream
	 * @param m
	 * @throws JAXBException 
	 * @throws IOException 
	 */
	public static void sendRequest(OutputStream out, Message msg) throws JAXBException, IOException{
		JAXBContext jc = JAXBContext.newInstance(Message.class,ObjectFactory.class,Utilisateur.class);
		Marshaller m = jc.createMarshaller();
		m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(msg,out);
		out.close();		
	}

	public static String convertToXMLString(Object o){
		StringWriter s = new StringWriter();
		JAXBContext jcontext;
		try {
			jcontext = JAXBContext.newInstance(AppConfigManager.getRessource("beans.package"));
			Marshaller ma = jcontext.createMarshaller();
			ma.marshal(o,s);
			System.out.println(s.toString());
			s.flush();
		} catch (JAXBException e) {
			logger.error(ExceptionsPropsManager.getRessource("convertString2XML.JAXBException")+" message:"+e);
		}
		return s.toString();
	}

	/**
	 * Receive response
	 * @param in
	 * @returnmanagement strategic
	 * 
	 * @throws JAXBException 
	 * @throws FileNotFoundException 
	 */
	public static Message receiveResponse(InputStream in) throws JAXBException, FileNotFoundException{
		return (Message) JAXBContext.newInstance(Message.class,ObjectFactory.class,Utilisateur.class).createUnmarshaller().unmarshal(in);
	}
}
